#include "sort.h"
#include "stack.h"
#include"queue.h"

void InsertSort(int* a, int n)
{
	// 升 序 排 序
	/*for (int i = 1; i <= n - 1; i++) {
		int j = i;
		while (a[j] < a[j - 1]&&j>0) {
			int tmp = a[j];
			a[j] = a[j - 1];
			a[j - 1] = tmp;
			j--;
		}
	}*///交换思路// 

	//下标(指针)思路
	for (int i = 1; i < n; i += 1) {
			//单趟[0,end]有序插入a[end+1]
		int end = i;
		int tmp = a[end];
		while (tmp<a[end-1]&&end>=1) {
			a[end] = a[end - 1];
			end--;
		}
		a[end] = tmp;
	}
}
void ShellSort(int* a, int n) {
	int gap = n;
	while (gap>1) {
		//gap>1时进行的是预排序
		gap = gap / 4 + 1;//最后一次排序是接近有序的插入排序
		for (int i = 0; i < n-gap; i++) {
			//单趟[0,end]有序插入a[end+1]
			int end = i+gap;
			int tmp = a[end];
			while ( end-gap>=0&&tmp < a[end - gap] ) {
				a[end] = a[end - gap];
				end-=gap;
			}
			a[end] = tmp;
		}	
	}	
}
void Swap(int* p1, int* p2) {
	int tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}
void SelectSort(int* a, int n) {
		int begin = 0;
		int end = n - 1;
	while (begin < end) {
		int max = begin;
		int min = begin;
		for (int i = begin; i <= end ; i++)
		{	
			if (a[max] < a[i])max = i;
			if (a[min] > a[i])min = i;	
		}
		if (min == end)min = max;
		Swap(&a[max], &a[end]);
		Swap(&a[min], &a[begin]);

		end--;
		begin++;
	}
}
//向下调整
void AdjustDown(int* a, int size, int parent) {
	int child = parent * 2 + 1;
	while (child < size) {
		if (child + 1 < size && a[child + 1] > a[child])child++;
		if (a[child] > a[parent]) {
			Swap(&a[child], &a[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
			break;
	}
}
void HeapSort(int* a, int n) {
	//建堆,升序排，建大堆
	//向下调整O(N)
	int parent = (n - 1 - 1) / 2;
	for (int i = parent; i >= 0; i--) {
		AdjustDown(a, n, i);
	}
	//排序O(N*logN)
	for (int i = 0; i < n-1; i++) {
		Swap(&a[0], &a[n - 1 - i]);
		AdjustDown(a, n - 1 - i, 0);
	}
}
void BubbleSort(int* a, int n) {
	for (int j = 0; j < n - 1; j++) {
		for (int i = 0; i < n-1-j; i++)
			if (a[i] > a[i + 1])Swap(&a[i], &a[i + 1]);
	}
}
//》》》》》》》》》》》》》快速排序《《《《《《《《《《《《《《
// 
// 三 数 取 中 优 化
void GetMid0(int* a, int begin, int end) {
	int left = a[begin];
	int mid = a[(begin + end) / 2];
	int right = a[end];
	if (left < mid && left < right) {
		if (mid <= right) {
			a[begin] = mid;
			a[(begin + end) / 2] = left;
		}
		else {
			a[begin] = right;
			a[end] = left;
		}
	}
	else if (left > mid && left > right) {
		if (mid >= right) {
			a[begin] = mid;
			a[(begin + end) / 2] = left;
		}
		else {
			a[begin] = right;
			a[end] = left;
		}
	}
}
int GetMid(int* a, int begin, int end) {
	int mid = (begin + end) / 2;
	if (a[mid] > a[begin]) {
		if (a[mid] < a[end])return mid;
		if (a[begin] > a[end])return begin;
		return end;
	}
	else {
		if (a[mid] > a[end])return mid;
		if (a[end] > a[begin])return end;
		return begin;
	}
}
//------------hoare 版 本----------------------
int QuickSort1(int* a, int begin, int end) {
	//优化一：三数取中
	GetMid0(a, begin, end);
	/*int mid = GetMid(a, begin, end);
	Swap(&a[mid], &a[begin]);*/

	int keyi = begin;
	int left = begin;
	int right = end;
	while (left < right) {
		while (left<right && a[right]>=a[keyi])--right;
		while (left<right && a[left]<=a[keyi])++left;
		if(left!=right)
		Swap(&a[left], &a[right]);
	}
	Swap(&a[keyi], &a[left]);
	keyi = left;
	return keyi;
}
//------------挖  坑  法----------------------
int QuickSort2(int* a, int begin, int end) {
	//优化一：三数取中
	/*int mid = GetMid(a, begin, end);
	Swap(&a[mid], &a[begin]);*/
	GetMid0(a, begin, end);
	int piti = begin;//第一个坑位
	int pit = a[begin];//储存变量
	int left = begin;
	int right = end;
	while (left < right) {
		while (left < right && a[right] >= a[piti])right--;
		a[piti] = a[right];
		piti = right;

		while (left < right && a[left] <= a[piti])left++;
		a[piti] = a[left];
		piti = left;
	}
	a[piti] = pit;
	return piti;
}
//-------------------前 后 指 针 法----------------------
int QuickSort3(int* a, int begin, int end) {
	//优化一：三数取中
	int mid = GetMid(a, begin, end);
	Swap(&a[mid], &a[begin]);

	int keyi = begin;
	int prev = begin;
	int cur = prev + 1;
	while (cur <= end) {
		if (a[cur] < a[keyi] && ++prev != cur)
			Swap(&a[prev], &a[cur]);
		++cur;
	}
	Swap(&a[keyi], &a[prev]);
	keyi = prev;
	return keyi;
}
// 
//int QuickSort3(int* a, int begin, int end) {
//	GetMid0(a, begin, end);
//	int key = a[begin];
//	int prev = begin;
//	int cur = begin + 1;
//	while (cur <= end) {
//		if (a[cur] < key && ++prev != cur)Swap(&a[prev], &a[cur]);
//		++cur;
//	}
//	Swap(&a[prev], &a[begin]);
//	return prev;
//}
void QuickSort(int* a, int begin, int end) {
	if (begin >= end)return;
	//优化二：小区间优化
	if (end - begin > 20) {
		int keyi = QuickSort3(a, begin, end);
		QuickSort(a, begin, keyi - 1);
		QuickSort(a, keyi + 1, end);
	}
	else {
		InsertSort(a + begin, end - begin + 1);
		//HeapSort(a + begin, end - begin + 1);
	}
}
//快排的非递归实现（借助栈）
void QuickSortNonR(int* a, int left, int right) {
	Stack s;
	StackInit(&s);
	StackPush(&s, left);
	StackPush(&s, right);
	while (!StackEmpty(&s)) {
		int r = StackTop(&s);
		StackPop(&s);
		int l = StackTop(&s);
		StackPop(&s);
		//小区间优化
		if (r-l<= 10) {
			InsertSort(a + l, r-l + 1);
		}
		else {
			int keyi = QuickSort2(a, l, r);
			if (l < keyi - 1) {
			StackPush(&s, l);
			StackPush(&s, keyi - 1);
			}
			if (r > keyi+1) {
			StackPush(&s, keyi+1);
			StackPush(&s, r);
			}

		}
		
	}
	StackDestroy(&s);
}
//void _MergeSort(int* a, int begin, int end, int *tmp) {
//	if (begin >= end)return;
//	int mid = (begin + end) / 2;
//	_MergeSort(a, begin, mid,tmp);
//	_MergeSort(a, mid+1, end,tmp);
//	int begin1 = begin, end1 = mid, begin2 = mid + 1, end2 = end;
//	int i = begin1;
//	while (begin1 <= end1 && begin2 <= end2) {
//		if (a[begin1] < a[begin2])tmp[i++] = a[begin1++];
//		else tmp[i++] = a[begin2++];
//	}
//	while (begin1 <= end1)tmp[i++] = a[begin1++];
//	while (begin2 <= end2)tmp[i++] = a[begin2++];
//	memcpy(a + begin, tmp + begin, sizeof(int) * (end - begin + 1));
//}
//void MergeSort(int* a, int n) {
//	int* tmp = (int*)malloc(sizeof(int) * n);
//	_MergeSort(a, 0, n - 1, tmp);
//	free(tmp);
//}
//void MergeSortNonR(int* a, int n) {
//	int* tmp = (int*)malloc(sizeof(int) * n);
//	if (tmp == NULL) {
//		perror("malloc fail");
//		exit(-1);
//	}
//	int begin = 0, end = n - 1;
//	int gap = 1;
//	while (gap < n) {
//		for (int j = 0; j < n; j += gap * 2) {
//			int i = j;
//			int begin1 = j, end1 = j+gap-1,
//				begin2 = j+gap, end2 = j+gap*2-1;
//			if (end1 >= n) {
//				end1 = n - 1;
//				begin2 = n;
//				end2 = n - 1;
//			}
//			else if (begin2 >= n) {
//				begin2 = n;
//				end2 = n - 1;
//			}
//			else if(end2>=n) {
//				end2 = n - 1;
//			}
//			while (begin1 <= end1 && begin2 <= end2) {
//				if (a[begin1] <= a[begin2])tmp[i++] = a[begin1++];
//				else tmp[i++] = a[begin2++];
//			}
//			while (begin1 <= end1)tmp[i++] = a[begin1++];
//			while (begin2 <= end2)tmp[i++] = a[begin2++];
//		}
//		memcpy(a + begin, tmp + begin, sizeof(int) * (end - begin + 1));
//		gap *= 2;
//	}
//	free(tmp);
//}

void _MergeSort(int* a, int begin, int end, int* tmp) {
	if (begin >= end)return;
	int mid = (begin + end) / 2;
	_MergeSort(a, begin, mid, tmp);
	_MergeSort(a, mid + 1, end, tmp);
	int begin1 = begin, end1 = mid;
	int begin2 = mid + 1, end2 = end;
	int i = begin;
	while (begin1 <= end1 && begin2 <= end2) {
		if (a[begin1] <= a[begin2])tmp[i++] = a[begin1++];
		else tmp[i++] = a[begin2++];
	}
	while (begin1 <= end1)tmp[i++] = a[begin1++];
	while (begin2 <= end2)tmp[i++] = a[begin2++];
	for (int j = begin; j <= end; ++j)a[j] = tmp[j];
}
void MergeSort(int* a, int n) {
	int* tmp = (int*)malloc(sizeof(int) * n);
	if (tmp == NULL) {
		perror("MergeSort malloc fail");
		exit(-1);
	}
	_MergeSort(a, 0, n - 1, tmp);
	free(tmp);
}
void MergeSortNonR(int* a, int n) {
	int* tmp = (int*)malloc(sizeof(int) * n);
	if (tmp == NULL) {
		perror("MergeSort malloc fail");
		exit(-1);
	}
	int gap = 1;
	while(gap<n){
	//[i,i+gap-1] [i+gap,i+2*gap-1]
		for (int i = 0; i < n; i += 2 * gap) {
			int begin1 = i, end1 = i + gap - 1;
			int begin2 = i + gap, end2 = i + 2 * gap - 1;
			int j = i;
			if (end1 >= n) {
				end1 = n - 1;
				begin2 = n - 1;
				end2 = n - 2;
			}
			if (begin2 >= n) {
				begin2 = n - 1;
				end2 = n - 2;
			}
			if (end2 >= n) {
				end2 = n - 1;
			}
			while (begin1 <= end1 && begin2 <= end2) {
				if (a[begin1] <= a[begin2])tmp[j++] = a[begin1++];
				else tmp[j++] = a[begin2++];
			}
			while (begin1 <= end1)tmp[j++] = a[begin1++];
			while (begin2 <= end2)tmp[j++] = a[begin2++];
			
		}
		for (int k = 0; k < n; ++k)a[k] = tmp[k];
		gap *= 2;
	}
	free(tmp);
}
void CountSort(int* a, int n) {
	int max = a[0];
	int min = a[0];
	for (int i = 1; i < n; i++) {
		if (a[i] > max)max = a[i];
		if (a[i] < min)min = a[i];
	}
	int range = max - min + 1;
	int* tmp = (int*)calloc(range, sizeof(int));
	if (tmp == NULL) {
		perror("countsort malloc fail");
		exit(-1);
	}
	for (int i = 0; i < n; ++i)tmp[a[i] - min]++;
	int j = 0;
	for (int i = 0; i < range; ++i)while (tmp[i]--)a[j++] = i + min;
	free(tmp);
}
void RedixSort(int* a, int n) {
	Queue Q[10];
	for (int i = 0; i < 10; ++i)
	{
		QueueInit(&Q[i]);
	}
	//10个队列 存储
	//k次（最大数位数）  
	//	   1.分发到10个桶
	//	   2.回收到原数组
	for (int i = 1; i < N; i*=10) {
		for (int j = 0; j < n; ++j) {
			QueuePush(&Q[(a[j] / i) % 10], a[j]);
		}
		int index = 0;
		for (int j = 0; j < 10; ++j) {
			while (!QueueEmpty(&Q[j])) {
				a[index++] = QueueFront(&Q[j]);
				QueuePop(&Q[j]);
			}
		}
	}
}